<%@page contentType="text/html;charset=UTF-8" language="java" import="com.compuvest.web.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<%@ taglib uri="/WEB-INF/tld/bottom.tld" prefix="bottom" %>
<%@ taglib uri="/WEB-INF/tld/head.tld" prefix="head" %>

<jsp:useBean id="sb" class="MySQL" scope="application"/>
<jsp:useBean id="advertArray" class="AdvertArray" scope="application"/>
<jsp:useBean id="mail" class="Mail" scope="application"/>
<jsp:useBean id="basket" class="Basket" scope="session"/>
<%!
    public static final String[] messagesCF = { 
/* 0 */	"<p>Your password is incorrect</p>",
/* 1 */	"<p>This email is not registered in our database</p>",
/* 2 */	"<p>Email or password cannot be empty</p>",
/* 3 */	"<p>You are a registered reseller. Please use 'Returning reseller' section above</p>",
/* 4 */	"<font color=green>Your password has been sent to the email entered</font>",
/* 5 */	"If you are a returning reseller, please log in to access your account.",
/* 6 */	"Choose a password for the ability to access your account in the future.",
/* 7 */	"Please enter your email address registered with your Compuvestb2b account. We will email you password shortly.",
/* 8 */	"<p>You registered but not reseller yet.</p>"
    };
%>
<%
    // logout
    if(request.getParameter("log") != null) {
	basket.setCustomer(null);
	basket.clean();
        session.invalidate();

	response.sendRedirect(application.getInitParameter("http"));
	return;
    }

    boolean nextPage = request.getParameter("n") == null ? false : true;
    if(basket.getCustomer() != null && basket.getReseller()) {
	response.sendRedirect(application.getInitParameter("https") + "CustomerEdit.jsp");
	return;
    }

    String formURL = application.getInitParameter("https") + "CustomerForm.jsp";

    int forms = 0;
    String errorMess = "";
    String email = advertArray.getString(request.getParameter("Email"), "");
    String password = advertArray.getString(request.getParameter("Password"), "");
    String mode = request.getParameter("mode");
    if(mode != null) {
	if("Go to my account".equals(mode)) {
	    forms = 1;
	    if(email.length() != 0 && password.length() != 0) {
		Customer cust = sb.getCustomer(email, password);
		if(cust.getCustomerID() > 0) { // present
                    boolean isReseller = "R".equals(cust.getType()) ? true : false;
                    if(isReseller) {
                        basket.setCustomer(cust);
                        basket.setReseller(isReseller);
        
                        if(nextPage) { // come from header
                            response.sendRedirect(application.getInitParameter("https") + "Payment.jsp");
                            return;
                        }
                        else {
                            response.sendRedirect(application.getInitParameter("https") + "CustomerEdit.jsp");
                            return;
                        }
                    }
                    else { // not reseller yet
                        errorMess = messagesCF[8];
                    }
		}
		else if(cust.getCustomerID() == 0) { // name present - passwd not correct
		    errorMess = messagesCF[0];
		}
		else if(cust.getCustomerID() == -1) { // new customer
		    errorMess = messagesCF[1];
		}
	    }
	    else errorMess = messagesCF[2];
	}
/*
        else if("Reseller profile".equals(mode)) {
	    forms = 2;
	    if(email.length() != 0 && password.length() != 0) {
		int result = sb.checkCustomer(email);
		if(result == 0) { // make new
		    Customer cust = new Customer();
		    cust.setEmail(email);
		    cust.setPassword(password);
		    cust.setCustomerID(0);
		    cust.billing.setCountry("US");
		    cust.shipping.setCountry("US");
		    cust.setType("C");

		    basket.setCustomer(cust);

                    response.sendRedirect(application.getInitParameter("https") + "Reseller.jsp");
		    return;
		}
		else if(result == -1) { // customer present
		    errorMess = messagesCF[3];
		}
	    }
	    else errorMess = messagesCF[2];
	}
*/
	else if("S u b m i t".equals(mode)) {
	    forms = 3;
	    if(email.length() != 0) {
		Customer cust = sb.getPassword(email);
		if(cust.getCustomerID() != 0) {
		    errorMess = messagesCF[4];
		    mail.sendPassword(cust); // send password
		}
		else {
		    errorMess = messagesCF[1];
		}
	    }
	    else errorMess = messagesCF[2];
	}
    }
%>
<html>
<%@include file="/incl/head.html" %>

<body>

<table style='height:100%;'>

<tr height=100>
 <td>

<head:show />

 </td>
</tr>

<%-- center --%>
<tr valign=top align=center>
 <td>
<br/><br/>

<form action="<%= formURL %>" method='POST' onSubmit='return isFormReadyOld(this);' enctype="application/x-www-form-urlencoded">
<%
    if(nextPage) { // come from header
%>
<input type='hidden' name='n' value='p'/>
<%
    }
%>
<table id='cfrm_tb'>
 <colgroup><col width='50%'><col width='50%'></colgroup>
 <tr><td class='bsm' colspan=2><span>Returning Reseller</span></td></tr>
 <tr height=90>
  <td>
   <div><%= (forms == 1) ? errorMess : messagesCF[5] %></div>
   <div>New reseller click <a href='<%= application.getInitParameter("https") + "Reseller.jsp" %>'><b>here</b></a>
    to set up an account now. Reseller Certificate required.</div>
  </td>
  <td class='brd'>
   <p>e-mail : <input maxlength=128 type='text' name='Email'/></p>
   <p>Password : <input maxlength=20 type='password' name='Password'/></p>
  </td>
 </tr>
 <tr height=26 class=bsm><td colspan=2 align=right><input type='submit' name='mode' value='Go to my account'/></td>
 </tr>
</table>
</form>

<%--
<form action="<%= formURL %>" method='POST' onSubmit='return isFormReady(this);' enctype="application/x-www-form-urlencoded">
<table id='cfrm_tb'>
 <colgroup><col width='50%'><col width='50%'></colgroup>
 <tr><td class=bsm colspan=2><span>New Reseller</span></td></tr>
 <tr height=90>
  <td><div><%= (forms == 2) ? errorMess : messagesCF[6] %></div></td>
  <td class='brd'>
   <p>e-mail : <input maxlength=128 type='text' name='Email'/></p>
   <p>Password : <input maxlength=20 type='password' name='Password'/></p>
   <p>Confirm Password : <input maxlength=20 type='password' name='vPassword'/></p>
  </td>
 </tr>
 <tr height=26><td class=bsm colspan=2 align=right><input type='submit' name='mode' value='Reseller profile'/></td></tr>
</table>
</form>
--%>

<form action="<%= formURL %>" method='POST' onSubmit='return isFormReadyFog(this);' enctype="application/x-www-form-urlencoded">
<table id='cfrm_tb'>
 <colgroup><col width='50%'><col width='50%'></colgroup>
 <tr><td class=bsm colspan=2><span>Forgot your password ?</span></td></tr>
 <tr height=90>
  <td><div><%= (forms == 3) ? errorMess : messagesCF[7] %></div></td>
  <td class='brd'>
   <p>e-mail : <input maxlength=128 type=text name="Email"/></p>
  </td>
 </tr>
 <tr height=26><td class=bsm colspan=2 align=right><input type='submit' name='mode' value='S u b m i t'/></td></tr>
</table>
</form>

 </td>
</tr>

<tr height=35>
 <td>

<bottom:show />

 </td>
</tr>

</table>

</body>
<%@ include file="./incl/tracking.incl" %>
</html>
